# Contributor: Sören Tempel <soeren+alpine@soeren-tempel.net>
# Maintainer: Sören Tempel <soeren+alpine@soeren-tempel.net>
pkgname=cyclone
pkgver=0.36.0
pkgrel=1
pkgdesc="A compiler for application development with R7RS Scheme"
url="https://justinethier.github.io/cyclone/"
# s390x, riscv64: ck on s390x does not have ck_pr_cas_8 (used by cyclone)
# loongarch64: fails tests
# See: https://github.com/concurrencykit/ck/issues/178
arch="all !s390x !riscv64"
license="MIT"
makedepends="ck-dev cyclone-bootstrap>=$pkgver strace gdb musl-dbg"
source="$pkgname-$pkgver.tar.gz::https://github.com/justinethier/cyclone/archive/refs/tags/v$pkgver.tar.gz
	gcc14.patch"

# Since cyclone itself is written in Scheme it needs to be bootstraped.
# For this propose, upstream provides a dedicated cyclone-bootstrap
# repository which includes C files generated by a previous cyclone
# version. We package this as cyclone-init-bootstrap in a separate
# package and compile the original cyclone Scheme sources using it
# initially. From that point onward, we build cyclone using itself.
#
# This is achieved by having both cyclone-init-bootstrap and cyclone
# provide the virtual cyclone-bootstrap package. The latter must have
# the higher provider priority.
#
# See also https://lists.alpinelinux.org/~alpine/devel/%3C33KG0XO61I4IL.2Z7RTAZ5J3SY6%408pit.net%3E
provides="cyclone-bootstrap=$pkgver"
provider_priority=100 # highest

export PREFIX=/usr
export DATADIR=$PREFIX/lib/cyclone
export CFLAGS="$CFLAGS -Wno-int-conversion"

# TODO: Tests pass on the CI but fail on the builders with:
#
#   icyc -p "(cond-expand (linux (begin (define-syntax foo (syntax-rules () ((foo) 123))) (foo))))"
#   Error: Unable to open input memory stream: 0
#   Call history, most recent first:
#   [1] scheme/base.sld:foldl
#   [2] icyc.scm:collect-opt-values
#   [3] scheme/base.sld:not
#   [4] scheme/base.sld:foldl
#   [5] icyc.scm:collect-opt-values
#   [6] scheme/base.sld:foldl
#   [7] icyc.scm:collect-opt-values
#   [8] scheme/base.sld:make-string
#   [9] scheme/cyclone/pretty-print.sld:lib-init:schemecyclonepretty_91print
#
case "$CARCH" in
loongarch64) options="!check" ;;
esac

build() {
	# We use a smaller pthread stack size by default,
	# make sure Cyclone configures the stack size manually.
	make CYC_PTHREAD_SET_STACK_SIZE=1
}

check() {
	make test
}

package() {
	DESTDIR="$pkgdir" make install
}

sha512sums="
6dfa3ff80b1a8397608483e6d914c25b1fee521d620991fb70f1bcf77303d290477100cbd82fbd9c939b5f2c91f72240f58711d9ab023721c15c8ad9b15998fd  cyclone-0.36.0.tar.gz
5a87f11593fe769517c933f6f1dcaf327212c9e053f07bd6cae40307b9aa5e796cbdb2d0e3b94c7240fdb002cb27a47fbb8919b0385be8686b9ff1b0db154dff  gcc14.patch
"
